﻿package CutLoad.mc
{
	/**
	 * 高级的翻页组件,包括有上一页，下一页，当前页按钮,前几页页码，后几页页码
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */
	import CutLoad.Base;
	import CutLoad.components.CButton;
	import CutLoad.components.container.ContainerH;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	
	public class PageBtnsSome extends Base
	{
		private var btnPageUp:Sprite,btnPageDown:Sprite,xspace:Number;
		//保存所有数字按钮
		private var arrNumMcToBtn:Array;
		private var mcBtnCurrentPage:McToBtn;
		//
		private var container:ContainerH;
		//当前页码
		private var currentPage:Number;
		private var pageCount:Number,classBtnPageDown:Function,classBtnPageUp:Function,classMcBtn:Function,showPageBtnNum:Number;
		//
		private var funPageChang:Function;
		/**
		 * 初始化  
		 * @param classBtnPageUp  生成上一页按钮的方法  格式:function():Base;
		 * @param classBtnPageDown 生成下一页按钮的方法 格式:function():Base;
		 * @param classMcBtn  生成显示页码的按钮 格式:function(页数:number):McToBtn;
		 * @param pageCount 总页数
		 * @param funPageChang 当翻页时调用的方法   格式:function(页数:Number) 返回记录对象数组
		 * @param currentPage 显示当前页
		 * @param showPageBtnNum 显示出当前面码的前几页和后几页
		 * @param xspace 间距
		 * 
		 */		
		public function PageBtnsSome(
			classBtnPageUp:Function,
			classBtnPageDown:Function,
			pageCount:Number,
			funPageChang:Function,
			classMcBtn:Function=null,
			currentPage:Number=1,
			showPageBtnNum:Number=3,
			xspace:Number=3
		)
		{
			arrNumMcToBtn=new Array();
			this.pageCount=pageCount;
			this.showPageBtnNum=showPageBtnNum;
			this.classBtnPageDown=classBtnPageDown;
			this.classBtnPageUp=classBtnPageUp;
			this.classMcBtn=classMcBtn;
			this.currentPage=currentPage;
			this.funPageChang=funPageChang;
			this.xspace=xspace;
			update();
		}
		/**
		 * 获得当前页 
		 * @return 
		 * 
		 */		
		public function getCurrentPage():Number{
			return this.currentPage;
		}
		/**
		 * 重新设置 
		 * @param pageCount 总页数
		 * @param currentPage 当前页数
		 * @return
		 * 
		 */		
		public function reset(pageCount:Number,currentPage:Number):void{
			this.pageCount=pageCount;
			this.currentPage=currentPage;
			update();
		}
		/**
		 * 设置当翻页时调用的方法   格式:function(页数:Number) 返回记录对象数组
		 * @param fun
		 * 
		 */		
		public function setFunPageChang(fun:Function):void{
			this.funPageChang=fun;
		}
		private function update():void{
			//清除之前的
			if(container!=null){
				if(container.parent!=null){
					this.removeChild(container);
				}
			}
			container=new ContainerH();
			container.init({w:0,isAuto:false,xspace:xspace,yspace:0});
			this.addChild(container);
			//
			//刷新，根据实际情况，显示出一个按钮
			//trace(currentPage,pageCount)
			if(currentPage==1&&this.pageCount==1){
				//不显示
			}else if(this.pageCount==2){
				//只显示上一页，下一页
				createPageUp();
				createPageDown();
			}else if(currentPage==1){
				createNumBtn()
				createPageDown();
			}else if(currentPage==pageCount){
				createPageUp();
				createNumBtn()
			}else{
				createPageUp();
				createNumBtn();
				createPageDown();
			}
			container.setTakeWH();
			//container.showGuide();
		}
		private function createPageUp():void{
			//清除之前的
			if(btnPageUp!=null){
				if(btnPageUp.hasEventListener(MouseEvent.CLICK)) btnPageUp.removeEventListener(MouseEvent.CLICK,funPageUp);
			}
			btnPageUp=classBtnPageUp()
			btnPageUp.addEventListener(MouseEvent.CLICK,funPageUp);
			container.append(Base.getBase(btnPageUp),false);
		}
		private function createPageDown():void{
			//清除之前的
			if(btnPageDown!=null){
				if(btnPageDown.hasEventListener(MouseEvent.CLICK)) btnPageDown.removeEventListener(MouseEvent.CLICK,funPageDown);
			}
			btnPageDown=classBtnPageDown()
			btnPageDown.addEventListener(MouseEvent.CLICK,funPageDown);
			container.append(Base.getBase(btnPageDown),false);
		}
		private function createNumBtn():void{
			//清除之前的
			for(var i:Number=0;i<arrNumMcToBtn.length;i++){
				var mcToBtn:McToBtn=arrNumMcToBtn[i];
				if(mcToBtn.hasEventListener(MouseEvent.CLICK)) mcToBtn.removeEventListener(MouseEvent.CLICK,funNumBtnClick);
			}
			var startI:Number=this.currentPage-showPageBtnNum;
			if(startI<=0) startI=1;
			var endI:Number=this.currentPage+showPageBtnNum;
			if(endI>this.pageCount) endI=this.pageCount;
			if(classMcBtn!=null){
				arrNumMcToBtn=new Array();
				for(i=startI;i<=endI;i++){
					var mcToBtnTem:McToBtn=classMcBtn(i) as McToBtn;
					mcToBtnTem.setOtherInfo({pageBtnSome_numBtnId:i});
					if(i==this.currentPage){
						mcToBtnTem.setStates(2);
					}
					mcToBtnTem.addEventListener(MouseEvent.CLICK,funNumBtnClick);
					arrNumMcToBtn.push(mcToBtnTem);
					container.append(mcToBtnTem,false);
				}
			}
			this.currentPage=currentPage;
		}
		private function funPageUp(e:MouseEvent):void{
			this.currentPage--;
			if(this.currentPage<=0){
				this.currentPage++;
				return ;
			}
			update();
			this.funPageChang(this.currentPage);
		}
		private function funPageDown(e:MouseEvent):void{
			this.currentPage++;
			if(this.currentPage>this.pageCount){
				this.currentPage--;
				return;
			}
			update();
			this.funPageChang(this.currentPage);
		}
		private function funNumBtnClick(e:MouseEvent):void{
			var obj:Object=e.target.getOtherInfo();
			//trace(obj.pageBtnSome_numBtnId)
			if(this.currentPage!=obj.pageBtnSome_numBtnId){
				this.currentPage=obj.pageBtnSome_numBtnId;
				this.funPageChang(currentPage);
			}
			update();
			funPageChang(this.currentPage);
		}
	}
}